// SceneView.cpp : implementation of the CSceneView class
//

#include "stdafx.h"
#include "ScenesTool.h"
#include "MemDC.h"
#include "SceneDoc.h"
#include "SceneView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define VIEW_ORG_X	16
#define VIEW_ORG_Y	16

/////////////////////////////////////////////////////////////////////////////
// CSceneView

IMPLEMENT_DYNCREATE(CSceneView, CScrollView)

BEGIN_MESSAGE_MAP(CSceneView, CScrollView)
	//{{AFX_MSG_MAP(CSceneView)
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSceneView construction/destruction

CSceneView::CSceneView()
{
	m_pMap = NULL;
	m_pSelect = NULL;
	m_nTimer = 0;
}

CSceneView::~CSceneView()
{
}

BOOL CSceneView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CScrollView::PreCreateWindow(cs);
}

void CSceneView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	sizeTotal.cx = sizeTotal.cy = 2048;
	SetScrollSizes(MM_TEXT, sizeTotal);

	VERIFY(m_brBkgnd.CreateSolidBrush(RGB(128, 128, 128)));
	VERIFY(m_brHatch.CreateHatchBrush(HS_CROSS, RGB(191, 191, 191)));

	m_nTimer = SetTimer(101, 333, NULL);

	CSceneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	list<GROUP*>::iterator it;
	it = pDoc->m_Groups.begin();
	while(it != pDoc->m_Groups.end())
	{
		GROUP* pGroup = *it++;

		list<TOBJECT*>::iterator lit;
		lit = pGroup->ObjList.begin();
		while(lit != pGroup->ObjList.end())
		{
			TOBJECT* pObject = *lit++;
			if(pObject->nType == CSceneDoc::tMap)
			{
				m_pMap = pObject;
				break;
			}
		}
		if(m_pMap != NULL)
		{
			break;
		}
	}

}

/////////////////////////////////////////////////////////////////////////////
// CSceneView drawing

void CSceneView::OnDraw(CDC* pDC)
{
	CSceneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	POINT ptOrg;
	ptOrg.x = VIEW_ORG_X;
	ptOrg.y = VIEW_ORG_Y;

	CSize tSize = GetTotalSize();

	RECT rect;
	GetClientRect(&rect);
	rect.left = 0;
	rect.top = 0;
	if(rect.right < tSize.cx)
		rect.right = tSize.cx;
	if(rect.bottom < tSize.cy)
		rect.bottom = tSize.cy;


	CMemDC* pMemDC = new CMemDC(pDC, rect);
	pMemDC->EraseBkgnd(m_brBkgnd, m_brHatch, VIEW_ORG_X, VIEW_ORG_Y);

	if(m_pMap != NULL && m_pMap->nType == CSceneDoc::tMap)
	{
		CMapKit::TMAP* pMap = m_pMap->pMap;
		pMap->OnDraw(pMemDC, ptOrg.x, ptOrg.y);
	}

	if(0 && m_pSelect != NULL)
	{
		switch(m_pSelect->nType)
		{
		case CSceneDoc::tSprite:
			{
				SPRITE* pSprite = m_pSelect->pSprite;
				if(pSprite)
				{
					int x = ptOrg.x + m_pSelect->nPosX;
					int y = ptOrg.y + m_pSelect->nPosY;
					pSprite->OnDraw(pMemDC, x, y, 0);
				}
			}
			break;
		}
	}
	if(0 && pDoc->m_Groups.size() > 0)
	{
		list<GROUP*>::iterator it;
		it = pDoc->m_Groups.begin();
		while(it != pDoc->m_Groups.end())
		{
			GROUP* pGroup = *it++;
			
			list<TOBJECT*>::iterator lit;
			lit = pGroup->ObjList.begin();
			while(lit != pGroup->ObjList.end())
			{
				TOBJECT* pObject = *lit++;
				if(pObject->nType == CSceneDoc::tSprite)
				{
					SPRITE* pSprite = pObject->pSprite;
					if(pSprite)
					{
						int x = ptOrg.x + pObject->nPosX;
						int y = ptOrg.y + pObject->nPosY;
						pSprite->OnDraw(pMemDC, x, y, 0);
					}
				}
			}
		}
	}
	
	delete pMemDC;	
}


/////////////////////////////////////////////////////////////////////////////
// CSceneView diagnostics

#ifdef _DEBUG
void CSceneView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSceneView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CSceneDoc* CSceneView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSceneDoc)));
	return (CSceneDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSceneView message handlers

BOOL CSceneView::OnEraseBkgnd(CDC* pDC) 
{
	return 1;
}

void CSceneView::OnDestroy() 
{
	CScrollView::OnDestroy();
	
	if(m_nTimer)	
	{
		KillTimer(m_nTimer);
		m_nTimer = 0;
	}
}

void CSceneView::OnTimer(UINT nIDEvent) 
{
	if(m_pSelect)	
	{
		if(m_pSelect->nType == CSceneDoc::tSprite)
		{
			SPRITE* pSprite = m_pSelect->pSprite;
			pSprite->OnUpdate();
			Invalidate();
		}
	}
	CScrollView::OnTimer(nIDEvent);
}

void CSceneView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(nFlags & MK_LBUTTON)
	{
		if(m_pSelect)
		{
			CPoint pt = point;
			pt.x -= VIEW_ORG_X;
			pt.y -= VIEW_ORG_Y;
			
			if(m_bDragging == FALSE)
			{
				m_ptOffset.x = pt.x - m_pSelect->nPosX;
				m_ptOffset.y = pt.y - m_pSelect->nPosY;

				m_bDragging = TRUE;
			}

			if(m_bDragging)
			{
				pt.x = pt.x - m_ptOffset.x;
				pt.y = pt.y - m_ptOffset.y;
				
				m_pSelect->nPosX = pt.x > 0 ? pt.x : 0;
				m_pSelect->nPosY = pt.y > 0 ? pt.y : 0;
				Invalidate();
			}
		}
	}
	else
	{
		m_bDragging = FALSE;
	}
	CScrollView::OnMouseMove(nFlags, point);
}
